import { getSecond , uuid} from '../../utils/common'
import { Account , Token , WorkChain , WorkNormal , AuthorContact , Author } from '../../models/'
import { CODE , TOKEN_TIME , PROGRESS_STATUS , WORK_CATEGORY , WORK_TYPE } from '../../config/config'

/**
 * @api {post} /author/authorList 获取作者列表
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName authorList
 * @apiGroup author
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/author/authorList'
 *
 * @apiParam {String}  token                   用户token
 * 
 * @apiSuccess {String}   count                作者数量
 * @apiSuccess {Array}    authors              作者信息集合
 * @apiSuccess {Object}   author               作者信息
 * @apiSuccess {String}   id                   作者id
 * @apiSuccess {String}   uid                  创建作者用户id 
 * @apiSuccess {String}   name                 作者姓名
 * @apiSuccess {String}   idcard               作者身份证
 * @apiSuccess {String}   phone                作者手机
 * @apiSuccess {String}   country              作者国家
 * @apiSuccess {String}   province             作者省
 * @apiSuccess {String}   job                  作者工作
 * @apiSuccess {String}   education            作者学历
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/author/authorList
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function authorList (ctx) {

	const token         = ctx.body.token
	const uid           = []
	if(token.phone_uid) {
		uid[uid.length] = token.phone_uid
	}
	if(token.wechat_uid) {
		uid[uid.length] = token.wechat_uid
	}
	if(token.qq_uid) {
		uid[uid.length] = token.qq_uid
	}

	const authors       = await Author.findAll({where : {uid,status:1}})
	const count         = await Author.count({where : {uid,status:1}})
	ctx.body            = CODE.success({authors,count})
}

/**
 * @api {post} /author/createAuthor 创建作者
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName createAuthor
 * @apiGroup author
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/author/createAuthor'
 *
 * @apiParam {String}  token                 用户token
 * @apiParam {String}   name                 作者姓名
 * @apiParam {String}   idcard               作者身份证
 * @apiParam {String}   phone                作者手机
 * @apiParam {String}   country              作者国家
 * @apiParam {String}   province             作者省
 * @apiParam {String}   education            作者学历
 * @apiParam {String}   job                  作者工作
 * @apiParam {String}   ext                  拓展字段
 * 
 * @apiSuccess {Object}   author               作者信息
 * @apiSuccess {String}   id                   作者id
 * @apiSuccess {String}   uid                  创建作者用户id 
 * @apiSuccess {String}   name                 作者姓名
 * @apiSuccess {String}   idcard               作者身份证
 * @apiSuccess {String}   phone                作者手机
 * @apiSuccess {String}   country              作者国家
 * @apiSuccess {String}   province             作者省
 * @apiSuccess {String}   job                  作者工作
 * @apiSuccess {String}   education            作者学历
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/author/createAuthor
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function createAuthor (ctx) {

	const token               = ctx.body.token
	const uid                 = token.phone_uid || token.wechat_uid || token.qq_uid
	
	const name = ctx.request.body.name
	const idcard = ctx.request.body.idcard
	const phone = ctx.request.body.phone
	const country = ctx.request.body.country
	const province = ctx.request.body.province
	const job = ctx.request.body.job
	const education = ctx.request.body.education
	const ext = ctx.request.body.ext
	const ctime = getSecond()

	const author = await Author.create({id:uuid(),name,idcard,phone,country,province,job,education,ext,ctime,uid})
	ctx.body            = CODE.success({author})
}

/**
 * @api {post} /author/updateAuthor 修改作者信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName updateAuthor
 * @apiGroup author
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/author/updateAuthor'
 *
 * @apiParam {String}  token                   用户token
 * @apiParam {String}   id                   作者id
 * @apiParam {String}   name                 作者姓名
 * @apiParam {String}   idcard               作者身份证
 * @apiParam {String}   phone                作者手机
 * @apiParam {String}   country              作者国家
 * @apiParam {String}   province             作者省
 * @apiParam {String}   education            作者学历
 * @apiParam {String}   job                  作者工作
 * @apiParam {String}   ext                  拓展字段
 * 
 * @apiSuccess {Object}   author               作者信息
 * @apiSuccess {String}   id                   作者id
 * @apiSuccess {String}   uid                  创建作者用户id 
 * @apiSuccess {String}   name                 作者姓名
 * @apiSuccess {String}   idcard               作者身份证
 * @apiSuccess {String}   phone                作者手机
 * @apiSuccess {String}   country              作者国家
 * @apiSuccess {String}   province             作者省
 * @apiSuccess {String}   job                  作者工作
 * @apiSuccess {String}   education            作者学历
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * @apiSampleRequest http://copyright.hotchain.vip/api/author/updateAuthor
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function updateAuthor (ctx) {

	const token         = ctx.body.token
	const id            = ctx.request.body.id
	const uid           = []
	if(token.phone_uid) {
		uid[uid.length] = token.phone_uid
	}
	if(token.wechat_uid) {
		uid[uid.length] = token.wechat_uid
	}
	if(token.qq_uid) {
		uid[uid.length] = token.qq_uid
	}
	const author        = await Author.findOne({where:{uid,status:1,id}})
	if(!author) {
		ctx.body        = CODE.ERROR.ERROR_AUTHOR_NOT_FOUNT
		return
	}
	const name          = ctx.request.body.name
	const idcard        = ctx.request.body.idcard
	const phone         = ctx.request.body.phone
	const country       = ctx.request.body.country
	const province      = ctx.request.body.province
	const job           = ctx.request.body.job
	const education     = ctx.request.body.education
	const ext           = ctx.request.body.ext
	author.name         = name || author.name
	author.idcard       = idcard || author.idcard
	author.phone        = phone || author.phone
	author.country      = country || author.country
	author.province     = province || author.province
	author.job          = job || author.job
	author.education    = education || author.education
	author.ext          = ext || author.ext
	await author.save()
	ctx.body            = CODE.success({author})
}

/**
 * @api {post} /author/deleteAuthor 删除作者信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName deleteAuthor
 * @apiGroup author
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/author/deleteAuthor'
 *
 * @apiParam {String}  token                   用户token
 * @apiParam {String}   id                     作者id
 * @apiSampleRequest http://copyright.hotchain.vip/api/author/deleteAuthor
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function deleteAuthor (ctx) {

	const token         = ctx.body.token
	const id            = ctx.request.body.id
	const uid           = []
	if(token.phone_uid) {
		uid[uid.length] = token.phone_uid
	}
	if(token.wechat_uid) {
		uid[uid.length] = token.wechat_uid
	}
	if(token.qq_uid) {
		uid[uid.length] = token.qq_uid
	}
	const author        = await Author.findOne({where:{uid,status:1,id}})
	if(!author) {
		ctx.body        = CODE.ERROR.ERROR_AUTHOR_NOT_FOUNT
		return
	}
	author.status       = 2
	await author.save()
	ctx.body            = CODE.success()
}


/**
 * @api {post} /author/updateWorkAuthor 更新作品作者信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName updateWorkAuthor
 * @apiGroup author
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/author/updateWorkAuthor'
 *
 * @apiParam {String}  token                   用户token
 * @apiParam {String}   id                   作品关联作者id contact_id
 * 
 * @apiParam {String}   author_id            作者id/id集合
 * @apiParam {String}   author_type          作者类型 1个人 2多人
 * @apiParam {String}   author_ext           作者关联信息拓展字段
 * 
 * 
 * @apiSuccess {Object}   authorContact        作者作品关联信息
 * @apiSuccess {String}   id             关联信息id
 * @apiSuccess {String}   uid            创建用户uid
 * @apiSuccess {String}   type           作者类型 1个人 2多人
 * @apiSuccess {String}   author_id      作者id/id集合
 * @apiSuccess {String}   ext            拓展字段
 * @apiSuccess {String}   ctime          创建时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/author/updateWorkAuthor
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function updateWorkAuthor (ctx) {

	const token         = ctx.body.token
	const id            = ctx.request.body.id

	const author_id     = ctx.request.body.author_id
	const author_type   = ctx.request.body.author_type
	const author_ext    = ctx.request.body.author_ext

	const uid           = []
	if(token.phone_uid) {
		uid[uid.length] = token.phone_uid
	}
	if(token.wechat_uid) {
		uid[uid.length] = token.wechat_uid
	}
	if(token.qq_uid) {
		uid[uid.length] = token.qq_uid
	}

	const authorContact  = await AuthorContact.findOne({
		where : {
			status:1,uid,id
		}
	})
	if(!authorContact) {
		ctx.body = CODE.ERROR.ERROR_AUTHOR_CONTACT_NOT_FOUNT
		return
	}
	authorContact.author_id   = author_id || authorContact.author_id
	authorContact.author_type = author_type || authorContact.author_type
	authorContact.ext         = author_ext || authorContact.ext
	await authorContact.save()
	ctx.body                  = CODE.success({authorContact})
}


/**
 * @api {post} /author/createWorkAuthor 创建作品作者信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName createWorkAuthor
 * @apiGroup author
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/author/createWorkAuthor'
 *
 * @apiParam {String}  token          用户token
 * @apiParam {String}   author_id     作者id/id集合
 * @apiParam {String}   type          作者类型 1个人 2多人
 * @apiParam {String}   ext           作者关联信息拓展字段
 * 
 * 
 * @apiSuccess {Object}   authorContact        作者作品关联信息
 * @apiSuccess {String}   id             关联信息id
 * @apiSuccess {String}   uid            创建用户uid
 * @apiSuccess {String}   type           作者类型 1个人 2多人
 * @apiSuccess {String}   author_id      作者id/id集合
 * @apiSuccess {String}   ext            拓展字段
 * @apiSuccess {String}   ctime          创建时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/author/createWorkAuthor
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function createWorkAuthor (ctx) {

	const token         = ctx.body.token

	const author_id     = ctx.request.body.author_id
	const type          = ctx.request.body.type
	const ext           = ctx.request.body.ext
	const id            = uuid()
	const ctime         = getSecond()
	const uid           = token.phone_uid || token.wechat_uid || token.qq_uid
	const authorContact = await AuthorContact.create({
		id,uid,type,author_id,ext,ctime
	})
	ctx.body            = CODE.success({authorContact})
}


export async function  checkToken(ctx,next) {
	const token 	  = ctx.request.body.token
	const tokenRes    = await Token.findOne({where:{token,status:1}})
	if(tokenRes) {
		if(tokenRes.ctime + TOKEN_TIME >= getSecond()) {
			ctx.body  = {token:tokenRes}
			if(next) {
				return next()
			}
		}
		ctx.body      = CODE.ERROR.ERROR_TOKEN_LOSE
		return
	}
	ctx.body          = CODE.ERROR.ERROR_TOKEN_NOT_CHECK
}


/**
 * @apiDefine ERROR
 * @apiErrorExample {json} 错误返回:
 *     {
 *       "code": "错误码",
 *       "error": "错误描述",
 *       "msg":"错误信息"
 *     }
 */